package com.zhuangfei.timetable.listener;


import com.zhuangfei.timetable.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;

/**
 * 控件实现的一个可以显示时间的侧边栏适配器
 * Created by Liu ZhuangFei on 2018/6/18.
 */
public class OnSlideBuildAdapter implements ISchedule.OnSlideBuildListener {

    /**
     * 时刻，每个元素保存每节课的开始时间
     */
    protected String[] times;

    /**
     * 节次文本的颜色、字号
     */
    protected int textColor = Color.BLACK.getValue();
    protected float textSize = 14;

    /**
     * 时刻文本的颜色、字号
     */
    protected float timeTextSize = 12;
    protected int timeTextColor = Color.GRAY.getValue();

    /**
     * 侧边栏背景色
     */
    protected RgbColor background = new RgbColor(255, 255, 255);
    protected float alpha = 1;

    /**
     * 设置时刻数组
     *
     * @param time
     *
     * @return OnSlideBuildAdapter
     */
    public OnSlideBuildAdapter setTimes(String[] time) {
        if (time != null) {
            this.times = time.clone();
        } else {
            this.times = null;
        }
        return this;
    }

    public OnSlideBuildAdapter setBackground(RgbColor backgroundColor) {
        this.background = backgroundColor;
        return this;
    }

    /**
     * 获取时刻数组
     *
     * @return String[]
     */
    public String[] getTimes() {
        if (times != null) {
            return times.clone();
        } else {
            return null;
        }
    }


    /**
     * 设置节次文本颜色
     *
     * @param textColor 指定颜色
     * @return OnSlideBuildAdapter
     */
    public OnSlideBuildAdapter setTextColor(int textColor) {
        this.textColor = textColor;
        return this;
    }

    /**
     * 设置节次文本的大小
     *
     * @param textSize 指定字号
     * @return OnSlideBuildAdapter
     */
    public OnSlideBuildAdapter setTextSize(float textSize) {
        this.textSize = textSize;
        return this;
    }

    /**
     * 设置节次时间的文本颜色
     *
     * @param timeTextColor 颜色
     * @return OnSlideBuildAdapter
     */
    public OnSlideBuildAdapter setTimeTextColor(int timeTextColor) {
        this.timeTextColor = timeTextColor;
        return this;
    }

    /**
     * 设置节次时间的文本大小
     *
     * @param timeTextSize 字号
     * @return OnSlideBuildAdapter
     */
    public OnSlideBuildAdapter setTimeTextSize(float timeTextSize) {
        this.timeTextSize = timeTextSize;
        return this;
    }


    @Override
    public Component getView(int pos, LayoutScatter inflater, int itemHeight, int marTop) {
        Component view = inflater.parse(ResourceTable.Layout_item_slide_time, null, false);
        Text numberTextView = (Text) view.findComponentById(ResourceTable.Id_item_slide_number);
        Text timeTextView = (Text) view.findComponentById(ResourceTable.Id_item_slide_time);


        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_CONTENT, itemHeight);
        lp.alignment = LayoutAlignment.CENTER;
        lp.setMargins(0, marTop, 0, 0);

        view.setLayoutConfig(lp);

        numberTextView.setText((pos + 1) + "");
        numberTextView.setTextColor(new Color(textColor));
        if (times == null) {
            timeTextView.setText("");
        }
        if (times != null && pos >= 0 && pos < times.length) {
            timeTextView.setText(times[pos]);
            timeTextView.setTextColor(new Color(timeTextColor));
        }
        return view;
    }

    @Override
    public void onInit(DirectionalLayout layout, float alpha) {
        this.alpha = alpha;
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(background);
        shapeElement.setAlpha((int) alpha);
        if (layout != null) {
            layout.setBackground(shapeElement);
        }
    }
}
